package com.mingmen.canting.chushizhang;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.activity.YuCaiGouDanActivity;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.definerecycleview.OnItemClickListener;
import com.mingmen.canting.definerecycleview.SwipeRecyclerView;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.ChushiZhangRight;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;
import com.mingmen.canting.util.DividerItemDecoration;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.MyStringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

public class ChushiZhangRightFragment extends BaseFragment {
    private SwipeRecyclerView mRecyclerView;
    private MychuShiRightAdapter mAdapter;
    private List<ChushiZhangRight> mDataList = new ArrayList<>();
    ;
    private String mTitle;


    private class NetWorkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //系统服务类
            String pos = intent.getStringExtra("pos");
            Log.d("pospospos", pos);
            if (mDataList.size() > 0) {
                mDataList.clear();
            }

            ChushiZhangRight chushiZhangRight = new ChushiZhangRight();
            chushiZhangRight = GsonUtil.GsonToBean(pos, ChushiZhangRight.class);
            List<ChushiZhangRight.DetailsBean> detailsBeanList = new ArrayList<>();
            List<ChushiZhangRight> chushiZhangRightList = new ArrayList<>();
            detailsBeanList = chushiZhangRight.getDetails();
            for (int i = 0; i < 1; i++) {
                ChushiZhangRight chushiZhangRight1 = new ChushiZhangRight();
                chushiZhangRight1.setApplyId(chushiZhangRight.getApplyId());
                chushiZhangRight1.setApplyUser(chushiZhangRight.getApplyUser());
                chushiZhangRight1.setExpanded(chushiZhangRight.isExpanded());
                chushiZhangRight1.setLimitDate(chushiZhangRight.getLimitDate());
                chushiZhangRight1.setDetails(detailsBeanList);
                mDataList.add(chushiZhangRight1);
            }

            mAdapter.setGroupList(mDataList);
            mAdapter.notifyDataSetChanged();
        }
    }

    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;

    public static ChushiZhangRightFragment getInstance(String title) {
        ChushiZhangRightFragment sf = new ChushiZhangRightFragment();
        sf.mTitle = title;
        return sf;
    }

    private void init() {

        intentFilter = new IntentFilter();
        intentFilter.addAction("canting.chushizhang.ChushiZhangRightFragment");
        netWorkChangeReceiver = new NetWorkChangeReceiver();
        //注册广播
        getActivity().registerReceiver(netWorkChangeReceiver, intentFilter);
    }
    Unbinder unbinder;
    @BindView(R.id.btn_digndan)
    Button btn_digndan;
    @BindView(R.id.btn_suofang)
    Button btnSuofang;
    @BindView(R.id.img_list)
    ImageView imgList;
    @Override
    public View initView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_chushizhang_right, null);
        TextView card_title_tv = (TextView) view.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);
        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        unbinder = ButterKnife.bind(this, view);
//        mRecyclerView.addItemDecoration(new DefaultItemDecoration(ContextCompat.getColor(getActivity(), R.color.divider_color)));
        DividerItemDecoration itemDecorationHeader = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);
//        itemDecorationHeader.setDividerDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_main_bg_height_1));
//        mRecyclerView.addItemDecoration(itemDecorationHeader);
        String right = getArguments().getString("right");
        Log.d("+", right);

        mRecyclerView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                // 根据原position判断该item是否是parent item
                if (mAdapter.isParentItem(position)) {
                    // 换取parent position
                    final int parentPosition = mAdapter.parentItemPosition(position);
                    // 判断parent是否打开了二级菜单
                    if (mAdapter.isExpanded(parentPosition)) {
                        mDataList.get(parentPosition).setExpanded(false);
                        mAdapter.notifyParentChanged(parentPosition);
                        // 关闭该parent下的二级菜单
                        mAdapter.collapseParent(parentPosition);
                    } else {
                        mDataList.get(parentPosition).setExpanded(true);
                        mAdapter.notifyParentChanged(parentPosition);
                        // 打开该parent下的二级菜单
                        mAdapter.expandParent(parentPosition);
                    }
                } else {
                    // 换取parent position
                    int parentPosition = mAdapter.parentItemPosition(position);
                    // 换取child position
                    int childPosition = mAdapter.childItemPosition(position);
                    String message = String.format("我是%1$d爸爸的%2$d儿子", parentPosition, childPosition);
//                    Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
                    mAdapter.setCheckedPosition(childPosition, parentPosition);
                }
            }


        });

        mAdapter = new MychuShiRightAdapter(getActivity());
        mRecyclerView.setAdapter(mAdapter);
        if (right.equals("0")) {

        } else {
            ChushiZhangRight chushiZhangRight = new ChushiZhangRight();
            chushiZhangRight = GsonUtil.GsonToBean(right, ChushiZhangRight.class);
            List<ChushiZhangRight.DetailsBean> detailsBeanList = new ArrayList<>();
            List<ChushiZhangRight> chushiZhangRightList = new ArrayList<>();
            detailsBeanList = chushiZhangRight.getDetails();
            for (int i = 0; i < 1; i++) {
                ChushiZhangRight chushiZhangRight1 = new ChushiZhangRight();
                chushiZhangRight1.setApplyId(chushiZhangRight.getApplyId());
                chushiZhangRight1.setApplyUser(chushiZhangRight.getApplyUser());
                chushiZhangRight1.setExpanded(chushiZhangRight.isExpanded());
                chushiZhangRight1.setLimitDate(chushiZhangRight.getLimitDate());


                chushiZhangRight1.setDetails(detailsBeanList);

                mDataList.add(chushiZhangRight1);
            }
        }


        mAdapter.setGroupList(mDataList);
        mAdapter.notifyDataSetChanged();

        init();

        return view;
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.btn_suofang,R.id.btn_digndan, R.id.img_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_suofang:
                btn_digndan.setVisibility(View.GONE);
                btnSuofang.setVisibility(View.VISIBLE);
                imgList.setVisibility(View.VISIBLE);
                break;

            case R.id.btn_digndan:
                startActivity(new Intent(getActivity(), ChuShiZhangNewDingDanActivity.class));
                break;
            case R.id.img_list:
                imgList.setVisibility(View.GONE);
                btn_digndan.setVisibility(View.VISIBLE);
                btnSuofang.setVisibility(View.VISIBLE);
                break;


        }
    }
}
